package com.meihua.shellapp.activity.wallet;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.aspsine.irecyclerview.IRecyclerView;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.meihua.shellapp.R;
import com.meihua.shellapp.adapter.personalcenter.MyIntegralAdapter;
import com.meihua.shellapp.base.BaseActivity;
import com.meihua.shellapp.base.MyApplication;
import com.meihua.shellapp.bean.IntegralBean;
import com.meihua.shellapp.databean.IntegralDataBean;
import com.meihua.shellapp.utils.MyStringCallBack;
import com.meihua.shellapp.utils.ToastUtils;
import com.meihua.shellapp.utils.URL;
import com.meihua.shellapp.view.footer.LoadMoreFooterView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

import static com.meihua.shellapp.R.id.iv_top_left;

/**
 * 我的积分
 */
public class IntegralActivity extends BaseActivity {

    @Bind(R.id.rv)
    IRecyclerView rv;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(iv_top_left)
    ImageView mIVTopLeft;

    private LoadMoreFooterView loadMoreFooterView;
    private MyIntegralAdapter mAdapter;
    private List<IntegralDataBean> data = new ArrayList<>();
    private int mPage = 1;

    @Override
    public int getLayoutId() {
        return R.layout.activity_layout_rv;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        tvTitle.setVisibility(View.VISIBLE);
        tvTitle.setText("我的积分");
        mIVTopLeft.setVisibility(View.VISIBLE);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        rv.setLayoutManager(layoutManager);
        loadMoreFooterView = (LoadMoreFooterView) rv.getLoadMoreFooterView();
        mAdapter = new MyIntegralAdapter(this, data);
        rv.setIAdapter(mAdapter);
        rv.setOnLoadMoreListener(this);
        rv.setOnRefreshListener(this);
    }

    @Override
    public void createLoadData() {
        super.createLoadData();
        getData();
    }

    /**
     * 获取数据
     */
    private void getData() {
        OkGo.<String>post(URL.getUrl().GETINTEGRS)
                .params("m_number", MyApplication.getInstance().getUserId())
                .params("page", mPage)
                .execute(new MyStringCallBack<IntegralBean>(rv) {

                    @Override
                    public void onSuccessData(IntegralBean bean) {
                        super.onSuccessData(bean);
                        if (bean.getData() != null && bean.getData().size() > 0) {
                            if (mPage == 1) {
                                data.clear();
                            }
                            data.addAll(bean.getData());
                            mAdapter.notifyDataSetChanged();
                        } else {
                            if (mPage == 1) {
                                data.clear();
                                mAdapter.notifyDataSetChanged();
                            }
                            endRefreshing(rv);
                            ToastUtils.getInstance().showToast(bean.getMsg());
                        }
                    }
                });
    }

    @OnClick(iv_top_left)
    public void onViewClicked() {
        finish();
    }

    @Override
    public void onRefresh() {
        super.onRefresh();
        mPage = 1;
        getData();
    }

    @Override
    public void onLoadMore() {
        super.onLoadMore();
        if (loadMoreFooterView.canLoadMore() && mAdapter.getItemCount() > 0) {
            loadMoreFooterView.setStatus(LoadMoreFooterView.Status.LOADING);
            mPage++;
            getData();
        }
    }
}
